<template>
    <el-menu :default-active="$route.name" class="el-menu-vertical-demo" @open="handleOpen" @close="handleClose"
        :collapse="isCollapse" background-color="#545c64" text-color="#fff" active-text-color="#ffd04b">
        <h3>{{ isCollapse ? '后台' : '教师管理系统' }}</h3>
        <el-menu-item @click="clickMenu(item)" v-for="item in noChild" :index="item.name" :key="item.name">
            <i :class="'el-icon-' + item.icon"></i>
            <span slot="title">{{ item.lable }}</span>
        </el-menu-item>
        <el-submenu v-for="item in hasChild" :index="item.name" :key="item.name" >
            <template slot="title">
                <i :class="'el-icon-' + item.icon"></i>
                <span>{{item.lable}}</span>
            </template>
            <!-- eslint-disable-next-line -->
            <el-menu-item-group  v-for="(subItem,subIndex) in item.children" :key="subItem.name">
                <el-menu-item @click="clickMenu(subItem)" :index="subItem.name">
                {{subItem.lable}}</el-menu-item>
            </el-menu-item-group>
        </el-submenu>
    </el-menu>
</template>

<script>
export default {
    // 组件名称
    name: 'commonAside',
    data() {
        return {
            currentName:'',
            teacherMenu: [
                {
                    path: '/',
                    name: 'home',
                    lable: '主页',
                    icon: 's-home',
                    url: 'Home/Home'
                },
                { 
                    name: 'detial',
                    lable: '成果登记',
                    icon: 'notebook-1',
                    children: [
                        {
                            path: '/teachArch',
                            name: 'teachArch',
                            lable: '教学成果登记',
                            url: 'TeacherManage/TeachArch',
                        },
                        {
                            path: '/scArch',
                            name: 'scArch',
                            lable: '科研成果登记',
                            url: 'TeacherManage/ScArch',
                        },
                    ]
                },
                {
                    path: '/password',
                    name: 'password',
                    lable: '密码管理',
                    icon: 'setting',
                    url: 'Password/Password'
                },
            ],
            manageMenu: [
                {
                    path: '/',
                    name: 'home',
                    lable: '主页',
                    icon: 'pie-chart',
                    url: 'Home/Home'
                },
                {
                    name:'rightsManage',
                    lable: '权限管理',
                    icon: 'setting',
                    children: [
                        {
                            path: '/userManage',
                            name: 'userManage',
                            lable: '用户管理',
                            url: 'RightsManage/UserManage',
                        },
                    ]
                },
                {
                    name: 'systemManage',
                    lable: '系统管理',
                    icon: 'setting',
                    children: [
                        {
                            path: '/password',
                            name: 'password',
                            lable: '修改密码',
                            url: 'SystemManage/Password',
                        }
                    ]
                },
                {
                    path: '/mangementMain',
                    name: 'mangementMain',
                    lable: '教师管理',
                    icon: 'setting',
                    url: 'TeacherManage/TeacherManage',
                }
            ],

        };
    },
    methods: {
        handleOpen(key, keyPath) {
            console.log(key, keyPath);
        },
        handleClose(key, keyPath) {
            console.log(key, keyPath);
        },
        clickMenu(item) {
            this.currentName = item.name
            this.$router.push({
                name: item.name
            })
            this.$store.commit('selectMenu', item)
        },
    },
    computed: {
        hasChild() {
            if (localStorage.getItem('user') === 'teacher') {
                return this.teacherMenu.filter(item => item.children)
            }
            else {
                return this.manageMenu.filter(item => item.children)
            }

        },
        noChild() {
            if (localStorage.getItem('user') === 'teacher') {
                return this.teacherMenu.filter(item => !item.children)
            }
            else {
                return this.manageMenu.filter(item => !item.children)
            }

        },
        isCollapse() {
            return this.$store.state.tab.isCollapse
        }
    }
}
</script> 

<style scoped lang='less'>
.el-menu-vertical-demo:not(.el-menu--collapse) {
    width: 200px;
    min-height: 300px;
}

.el-menu {
    height: 100%;
    border: none;

    h3 {
        line-height: 60px;
        color: white;
        text-align: center;
    }
}
</style>
